package com.link510.aitools.services;


import com.blankj.utilcode.util.DeviceUtils;
import com.link510.aitools.core.config.AppDefalutConfig;
import com.link510.aitools.core.domain.AppMachineInfo;
import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;


/**
 * 应用
 */
public class Commons {

    private static final String TAG = "Machines";

    private static final String Machines_Keys = "AppMachineInfos";

    private static Commons mInstance = null;

    public static synchronized Commons getInstance() {

        if (mInstance == null) {
            mInstance = new Commons();
        }


        return mInstance;
    }

    private Commons() {
        Logger.e("u can initializing me...");
    }

//    public void loginMachine(String deviceToken) {
//
//        try {
//
//            AppMachineInfo machineInfo = loadMachineInfo();
//
//            Map<String, String> parms = new HashMap<>();
//
//            parms.put("deviceToken", deviceToken);
//            parms.put("mac", machineInfo.getMac());
//            parms.put("name", machineInfo.getName());
//            parms.put("deviceSN", machineInfo.getDeviceSN());
//            parms.put("packName", "com.link510.alarm");
//            parms.put("runtime", machineInfo.getRunTime().toString());
//
//
//            VolleyHelper.getInstance().request(machineInfo.getApiUrl(), parms, new Response.Listener<String>() {
//                @Override
//                public void onResponse(String response) {
//                     Logger.e( "onResponse: " + response.toString());
//                }
//            }, new Response.ErrorListener() {
//                @Override
//                public void onErrorResponse(VolleyError error) {
//
//                     Logger.e( "onResponse: " + error.getMessage());
//                }
//            });
//
//        } catch (Exception ex) {
//
//        }
//
//    }

    /**
     * 加载机器信息
     *
     * @return AppMachineInfo
     */
    public AppMachineInfo loadMachineInfo() {

        try {

            AppMachineInfo machineInfo = Hawk.get(Machines_Keys);

            if (machineInfo == null) {
                return defaultMachineInfo();

            }

            return machineInfo;

        } catch (Exception ex) {
            Logger.e("加载机器信息", ex);
        }

        return defaultMachineInfo();

    }

    /**
     * 初始化机器信息
     *
     * @return AppMachineInfo
     */
    private AppMachineInfo initMachineInfo() {

        try {

            AppMachineInfo machineInfo = defaultMachineInfo();

            if (saveMachineInfo(machineInfo)) {
                return machineInfo;
            }
        } catch (Exception ex) {

            Logger.e("保存机器信息", ex);
        }

        return defaultMachineInfo();
    }

    /**
     * 保存机器信息
     *
     * @param machineInfo 机器
     * @return boolean
     */
    public boolean saveMachineInfo(AppMachineInfo machineInfo) {

        try {
            Hawk.put(Machines_Keys, machineInfo);
            return true;

        } catch (Exception ex) {

            Logger.e("保存机器信息", ex);

        }
        return false;
    }

    /**
     * 默认信息
     *
     * @return AppMachineInfo
     */
    public AppMachineInfo defaultMachineInfo() {

        AppMachineInfo machineInfo = new AppMachineInfo();

        machineInfo.setDeviceSN(DeviceUtils.getMacAddress().replace(":", ""));
        machineInfo.setName(DeviceUtils.getMacAddress());
        machineInfo.setMac(DeviceUtils.getMacAddress());
        machineInfo.setHost(AppDefalutConfig.getInstance().getHost());
        machineInfo.setPort(AppDefalutConfig.getInstance().getPort());
        machineInfo.setSecretKey(AppDefalutConfig.getInstance().getSecretKey());
        return machineInfo;
    }

    public static void main(String[] args) {


    }

}
